package cn.stylefeng.guns.modular.emergency.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.consts.ProjectConstants;
import cn.stylefeng.guns.modular.emergency.entity.RelationLevel;
import cn.stylefeng.guns.modular.emergency.mapper.RelationLevelMapper;
import cn.stylefeng.guns.modular.emergency.mapper.TreeEmergencyTypeMapper;
import cn.stylefeng.guns.modular.emergency.pojo.response.EmergencyResponse;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.emergency.entity.Emergency;
import cn.stylefeng.guns.modular.emergency.enums.EmergencyExceptionEnum;
import cn.stylefeng.guns.modular.emergency.mapper.EmergencyMapper;
import cn.stylefeng.guns.modular.emergency.pojo.request.EmergencyRequest;
import cn.stylefeng.guns.modular.emergency.service.EmergencyService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Date;
import java.util.stream.Collectors;

/**
 * 应急预案表	业务实现层
 *
 * @author xbz
 * @date 2023/04/24 14:58
 */
@Service
public class EmergencyServiceImpl extends ServiceImpl<EmergencyMapper, Emergency> implements EmergencyService {

    @Resource
    private TreeEmergencyTypeMapper treeEmergencyTypeMapper;
	@Override
    public ResponseData add(EmergencyRequest emergencyRequest) {
        List<RelationLevel> leafNodeList = treeEmergencyTypeMapper.getLeafNodeList(ProjectConstants.EPC_EMERGENCY_TYPE_TABLE_NAME);
        List<Long> collect = leafNodeList.stream().map(RelationLevel::getDataId).collect(Collectors.toList());

        if(collect.contains(emergencyRequest.getCategoryId())){
            Emergency emergency = new Emergency();
            BeanUtil.copyProperties(emergencyRequest, emergency);
            this.save(emergency);
            return new SuccessResponseData("添加成功");
        }else {
            return new ErrorResponseData("1001","该类别下有子类,不允许在该类下添加应急预案");
        }

    }

    @Override
    public void del(EmergencyRequest emergencyRequest) {
        Emergency emergency = this.queryEmergency(emergencyRequest);
        emergency.setDelFlag("Y");
        this.updateById(emergency);
    }

    @Override
    public ResponseData edit(EmergencyRequest emergencyRequest) {
        List<RelationLevel> leafNodeList = treeEmergencyTypeMapper.getLeafNodeList(ProjectConstants.EPC_EMERGENCY_TYPE_TABLE_NAME);
        List<Long> collect = leafNodeList.stream().map(RelationLevel::getDataId).collect(Collectors.toList());
        if(collect.contains(emergencyRequest.getCategoryId())){
            Emergency emergency = this.queryEmergency(emergencyRequest);
            BeanUtil.copyProperties(emergencyRequest, emergency);
            this.updateById(emergency);
            return new SuccessResponseData("修改成功");
        }else {
            return new ErrorResponseData("1001","该类别下有子类,不允许将应急预案放在该类别下面");
        }

    }

    @Override
    public Emergency detail(EmergencyRequest emergencyRequest) {
        return this.queryEmergency(emergencyRequest);
    }

    @Override
    public PageResult<Emergency> findPage(EmergencyRequest emergencyRequest) {
        LambdaQueryWrapper<Emergency> wrapper = createWrapper(emergencyRequest);
        Page<Emergency> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public PageResult<EmergencyResponse> getBelowEmergency(String tableName, Long id,String emergencyName,Integer pageSize,Integer pageNo) {
        List<EmergencyResponse> list = this.baseMapper.getBelowEmergency(tableName, id,emergencyName);
        return PageResultFactory.createPageResult(list,list.stream().count(), pageSize,pageNo);
    }

    @Override
    public List<Emergency> findList(EmergencyRequest emergencyRequest) {
        LambdaQueryWrapper<Emergency> wrapper = this.createWrapper(emergencyRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    private Emergency queryEmergency(EmergencyRequest emergencyRequest) {
        Emergency emergency = this.getById(emergencyRequest.getId());
        if (ObjectUtil.isEmpty(emergency)) {
            throw new ServiceException(EmergencyExceptionEnum.EMERGENCY_NOT_EXISTED);
        }
        return emergency;
    }

    /**
     * 创建查询wrapper
     *
     * @author xbz
     * @date 2023/04/24 14:58
     */
    private LambdaQueryWrapper<Emergency> createWrapper(EmergencyRequest emergencyRequest) {
        LambdaQueryWrapper<Emergency> queryWrapper = new LambdaQueryWrapper<>();

        Long id = emergencyRequest.getId();
        Long categoryId = emergencyRequest.getCategoryId();
        String emergencyName = emergencyRequest.getEmergencyName();
        String emergencyCategory = emergencyRequest.getEmergencyCategory();
        Long fileId = emergencyRequest.getFileId();
        String emergencyInfo = emergencyRequest.getEmergencyInfo();

        queryWrapper.eq(ObjectUtil.isNotNull(id), Emergency::getId, id);
        queryWrapper.eq(ObjectUtil.isNotNull(categoryId), Emergency::getCategoryId, categoryId);
        queryWrapper.like(ObjectUtil.isNotEmpty(emergencyName), Emergency::getEmergencyName, emergencyName);
        queryWrapper.like(ObjectUtil.isNotEmpty(emergencyCategory), Emergency::getEmergencyCategory, emergencyCategory);
        queryWrapper.eq(ObjectUtil.isNotNull(fileId), Emergency::getFileId, fileId);
        queryWrapper.like(ObjectUtil.isNotEmpty(emergencyInfo), Emergency::getEmergencyInfo, emergencyInfo);

        return queryWrapper;
    }

}